from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from movie_manage_ui import Ui_movie_manage
from PyQt5.uic import loadUiType
import sys
ui, _ = loadUiType('movie_manage.ui')

class Movie_manage_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        #self.handle_ui_change()
        self.handle_buttons()
        self.show()
        self.show_tab()

    def handle_buttons(self):
        self.add_movie_record.clicked.connect(self.Add_movie_record)
        self.drop_movie_record.clicked.connect(self.Drop_movie_record)
        self.query_movie_record.clicked.connect(self.Query_movie_record)

    #1.添加类别
    def Add_movie_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.mange_movie_name.text()
        MovieDate = self.manaage_date.text()
        MovieTime = self.manage_time.text()
        MovieDiretor = self.manage_diretor.text()
        MovieActor = self.manage_actor.text()
        MovieKind = self.manage_movie_kind.text()
        KindMovie = self.kind_movie_line.currentText()
        if (MovieName== "" or MovieDate == "" or MovieTime == "" or MovieDiretor == "" or MovieActor == "" or MovieKind == "" or KindMovie == ""):
            self.statusBar().showMessage('字段为空添加有毛用！')
            return
        else:
            sql = "insert into movie_manage(MovieName,MovieDate,MovieTime,MovieDiretor,MovieActor,MovieKind,KindMovie) VALUES (%s,%s,%s,%s,%s,%s,%s)"
            values=(MovieName,MovieDate,MovieTime,MovieDiretor,MovieActor,MovieKind,KindMovie)
            cur.execute(sql, values)
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('好耶！添加成功!')
            self.show_tab()
            self.clearEdit()
        return



    def show_tab(self):
        conn=get_conn()
        cur=conn.cursor()
        sql="select * from movie_manage order by MovieDate asc"
        cur.execute(sql,)
        data = cur.fetchall()
        if data:
            self.manage_movie_table.setRowCount(0)
            self.manage_movie_table.insertRow(0)
            for row , form in enumerate(data):
                for column ,item in enumerate(form):
                    self.manage_movie_table.setItem(row,column,QTableWidgetItem(str(item)))
                    column += 1
                row_position=self.manage_movie_table.rowCount()
                self.manage_movie_table.insertRow(row_position)



    def Drop_movie_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName = self.mange_movie_name.text()
        MovieDate = self.manaage_date.text()
        MovieTime = self.manage_time.text()
        MovieDiretor = self.manage_diretor.text()
        MovieActor = self.manage_actor.text()
        MovieKind = self.manage_movie_kind.text()
        if (MovieName== ""):
            self.statusBar().showMessage('字段为空你要删啥啊！')
            return
        else:
            sql1 = "delete from movie_manage where MovieName=%s and MovieDate=%s and MovieTime=%s and MovieDiretor=%s and MovieActor=%s and MovieKind=%s"
            cur.execute(sql1, (MovieName,MovieDate,MovieTime,MovieDiretor, MovieActor,MovieKind))
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('你真棒还把爷删了。。。')
            self.show_tab()
            self.clearEdit()
        return


    def Query_movie_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieName=self.add_movie_record.text()
        MovieDate = self.manaage_date.text()
        MovieDiretor = self.manage_diretor.text()
        MovieActor = self.manage_actor.text()
        MovieKind = self.manage_movie_kind.text()
        sql2 = "select * from movie_manage where MovieName=%s or MovieDate=%s or MovieDiretor=%s or MovieActor=%s or MovieKind=%s"
        cur.execute(sql2, (MovieName,MovieDate,MovieDiretor,MovieActor,MovieKind))
        data = cur.fetchall()
        if data:
            self.manage_movie_table.setRowCount(0)
            self.manage_movie_table.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.manage_movie_table.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.manage_movie_table.rowCount()
                self.manage_movie_table.insertRow(row_position)
                self.statusBar().showMessage('老哥你要的结果。')
        else:
            self.statusBar().showMessage('搜不到搜不到！！！')



    def clearEdit(self):
        self.mange_movie_name.clear()
        self.manaage_date.clear()
        self.manage_time.clear()
        self.manage_diretor.clear()
        self.manage_actor.clear()
        self.manage_movie_kind.clear()

def main():
    app = QApplication([])
    window = Movie_manage_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()